/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */

const { Node, LinkedList } = require('./linkedList');
var rotateRight = function (head, k) {
  if (k === null || k == undefined || k == 0) return head
  let curr = head
  let n = 1 // 链表的长度
  while (curr.next) {
    curr = curr.next
    n++
  }
  let add = n - k % n;
  if (add === n) {
    return head;
  }

  curr.next = head;
  while (add) {
    curr = curr.next;
    add--;
  }

  const ret = curr.next;
  curr.next = null;
  return ret;
};
let head = new LinkedList()
head.append(1)
head.append(2)
head.append(3)
head.append(4)
head.append(5)
// console.log(head);
let rst = rotateRight(head.head, 2)
console.log(JSON.stringify(rst));